package com.hxkcloud.patient.aerotel.receiver;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.hxkcloud.patient.aerotel.util.ClsUtils;


public class BluetoothReceiver extends BroadcastReceiver {
    public static final String TAG = BluetoothReceiver.class.getSimpleName();

    //	String pin = "39121440";  //此处为你要连接的蓝牙设备的初始密钥，一般为1234或0000
    String pin = "39121440";

    public BluetoothReceiver() {

    }

    //广播接收器，当远程蓝牙设备被发现时，回调函数onReceiver()会被执行
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction(); //得到action
        Log.e("action1=", action);
        BluetoothDevice btDevice = null;  //创建一个蓝牙device对象
        // 从Intent中获取设备对象
        btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (btDevice==null){
            //没有蓝牙设备
            context.sendBroadcast(new Intent().setAction(ClsUtils.NO_DEVICES));
            Log.e(TAG,"sendBroadcast BlueToothNODevices");
        }

        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {  //扫描设备结束
                context.sendBroadcast(new Intent().setAction(ClsUtils.NO_DEVICES));
                Log.e(TAG,"sendBroadcast ACTION_DISCOVERY_FINISHED BlueToothNODevices");
        }else  if (BluetoothDevice.ACTION_FOUND.equals(action)) {  //发现设备
            Log.e(TAG, "发现设备 [" + btDevice.getName() + "]" + ":" + btDevice.getAddress());

            if (btDevice.getName() != null && btDevice.getName().contains("HVP12-8BT"))//HC-05设备如果有多个，第一个搜到的那个会被尝试。
            {
                if (btDevice.getBondState() == BluetoothDevice.BOND_NONE) {

                    Log.e("zts", "attemp to bond:" + "[" + btDevice.getName() + "]");
                    try {
                        //通过工具类ClsUtils,调用createBond方法
                        ClsUtils.createBond(btDevice.getClass(), btDevice);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else{
                context.sendBroadcast(new Intent().setAction(ClsUtils.NO_DEVICES));
                Log.e("BluetoothReceiver", "sendBroadcast BlueToothNODevices");
            }
//				Toast.makeText(context,"Aerotel设备未打开,请打开Aerotel设备",Toast.LENGTH_SHORT).show();
                Log.e("error", "Is faild");
        } else if (action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) { //再次得到的action，会等于PAIRING_REQUEST

            Log.e(TAG,"action2="+ action);
            if (btDevice.getName().contains("HVP12-8BT")) {//蓝牙的名字
                Log.e(TAG,"here  OKOKOK");

                try {
//
//                    //1.确认配对
//                    ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
                    //2.终止有序广播
                    Log.i(TAG,"order..."+ "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
                    abortBroadcast();//如果没有将广播终止，则会出现一个一闪而过的配对框。
                    //3.调用setPin方法进行配对...
                    Log.e(TAG,"BroadCast  aborted");
                    boolean ret = ClsUtils.setPin(btDevice.getClass(), btDevice, pin, context);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                Log.e("提示信息", "这个设备不是目标蓝牙设备");
        }
    }
}